package com.jwell.panprovider.ui.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.jwell.panprovider.R
import com.jwell.panprovider.model.PreRegisterModel
import kotlinx.android.synthetic.main.item_pre_register_driver_layout.view.*

/**
 * Created by 76577 on 2018/4/11.
 * 预登记增加司机
 */
class PreRegisterDriverAdapter constructor(list: ArrayList<PreRegisterModel.DriverInfo>) : RecyclerView.Adapter<PreRegisterDriverAdapter.ViewHolder>() {
    private var list = list
    private var chooseListener: ChooseListener? = null
    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_pre_register_driver_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            text_chooseCarNum.text = list[position].carNum
            text_driverName.text = list[position].driverName
            text_driverPhone.text = list[position].driverPhone
            text_chooseCarNum.setOnClickListener {
                chooseListener?.chooseCarNum(position)
            }
            text_driverName.setOnClickListener {
                chooseListener?.chooseDriver(position)
            }
            text_driverPhone.setOnClickListener {
                chooseListener?.chooseDriver(position)
            }
            text_delete.setOnClickListener {
                if (list.size > 1) {
                    list.removeAt(position)
                    notifyDataSetChanged()
                } else {
                    Toast.makeText(holder.itemView.context, "至少调度一位司机", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    interface ChooseListener {
        //选择车牌
        fun chooseCarNum(position: Int)

        //选择司机信息
        fun chooseDriver(position: Int)
    }

    fun setChooseListener(listener: ChooseListener) {
        this.chooseListener = listener
    }
}